<?php
/**
* User: zhyu
 * Date: 2017年3月28日
 *描述
 *
 *　　　　　　　　┏┓　　　┏┓+ +
 *　　　　　　　┏┛┻━━━┛┻┓ + +
 *　　　　　　　┃　　　　　　　┃ 　
 *　　　　　　　┃　　　━　　　┃ ++ + + +
 *　　　　　　 ████━████ ┃+
 *　　　　　　　┃　　　　　　　┃ +
 *　　　　　　　┃　　　┻　　　┃
 *　　　　　　　┃　　　　　　　┃ + +
 *　　　　　　　┗━┓　　　┏━┛
 *　　　　　　　　　┃　　　┃　　　　　　　　　　　
 *　　　　　　　　　┃　　　┃ + + + +
 *　　　　　　　　　┃　　　┃　　　　Code is far away from bug with the animal protecting　　　　　　　
 *　　　　　　　　　┃　　　┃ + 　　　　神兽保佑,代码无bug　　
 *　　　　　　　　　┃　　　┃
 *　　　　　　　　　┃　　　┃　　+　　　　　　　　　
 *　　　　　　　　　┃　 　　┗━━━┓ + +
 *　　　　　　　　　┃ 　　　　　　　┣┓
 *　　　　　　　　　┃ 　　　　　　　┏┛
 *　　　　　　　　　┗┓┓┏━┳┓┏┛ + + + +
 *　　　　　　　　　　┃┫┫　┃┫┫
 *　　　　　　　　　　┗┻┛　┗┻┛+ + + +
  *         ━━━━━━感觉萌萌哒━━━━━━
 */
class Statistic extends PC_Controller{
    protected $go_url = '/admin/statistic/user_list';
    public function __construct(){
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('area_model');
    }
    /** 
    * some_func  
    * 业绩统计列表
    * 
    * @access public 
    * @param mixed 
    * @param mixed 
    * @param mixed 
    * @since 1.0 
    * @return array 
    */  
    public function user_list() {
        $this->data['username'] =  empty($_POST) ? '' : $_POST['username'];
//         $this->data['phone'] =  empty($_POST) ? '' : $_POST['phone'];
        $this->data['id']=  isset($_REQUEST['id'])?$_REQUEST['id']:'';
        $this->data['start_time']= isset($_REQUEST['start_time'])&& $_REQUEST['start_time']?$_REQUEST['start_time']:date('Y-m-d 0:0:0',strtotime('-7 day'));
        $this->data['end_time']= isset($_REQUEST['end_time'])&& $_REQUEST['end_time']?$_REQUEST['end_time']:date('Y-m-d 23:59:59');
        //查询当前用户信息就是所有人下级的信息
        $user=array();
        if(isset(  $this->data['username']) &&   $this->data['username']){
            $user=$this->user_model->checkUser(array('user.username'=>  $this->data['username']));
        }else if(isset($_REQUEST['id']) && $_REQUEST['id']){
            $user=$this->user_model->checkUser(array('user.id'=> $_REQUEST['id']));
        }
        if(isset($user['id']) && $user['id']){
            $this->data['username']=$user['username'];
            $where='FIND_IN_SET (\''.$user['id'].'\',parent_path) and id!='.$user['id'];
            $this->data['start_time']?$where.=' and s_time >='.strtotime($this->data['start_time']):'';
            $this->data['end_time']?$where.=' and user.s_time <='.strtotime($this->data['end_time']):'';
            $user_tmp=$this->user_model->getFeildList($where,'count(user.id) as total_next_count  ,sum(user.price) as total_price');
            if(isset($user_tmp[0]['total_next_count'])){
                $user['next_count']=$user_tmp[0]['total_next_count']?$user_tmp[0]['total_next_count']:0;
                $user['performance']=$user_tmp[0]['total_price']?$user_tmp[0]['total_price']:0;
            }
        }
        //根据时间查询当前下级个数和业绩
        
        $this->data['user']=$user;
        $this->data['message']='1';
        $this->load->view('/admin/statistic_list',$this->data);
    } 
    
    /**
     * @brief ajxa获取会员列表数据
     * @param Null
     * @par 2015/10/12 Ver 1.00 Created by Allen
     */
    public function ajax_user_list() {
    
        $data = $_GET;
        $start = $data['iDisplayStart']; //显示的起始索引
        $length = $data['iDisplayLength'];//显示的行数
        $sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列
        $sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
        $search = $data ['sSearch'];//全局搜索字段
        if(isset( $data['username']) &&  $data['username']){
            $user=$this->user_model->checkUser(array('user.username'=> $data['username']));
           
            if(isset($user['id']) && $user['id']){
                $data['id']=$user['id'];
            }
        }
       
        $data['start_time']?$where['user.s_time >=']=strtotime($data['start_time']):'';
        $data['end_time']?$where['user.s_time <=']=strtotime($data['end_time']):'';
        $where['concat(user.id) like'] = '%'.trim($search).'%';
        $this->data['count']=0;
        if(isset( $data['id']) && $data['id']){
            $where['user.parent_id']=$data['id'];
            $this->data['count'] = $this->user_model->getCount($where);
            
        }
        
        //$group_performance=$this->user_model->select('sum(price) as group_performance')->find();
        $aaData = array();
        $user=array();
        if($this->data['count']) {
            $user=$this->user_model->checkUser(array('user.username'=> $data['username']));
            $order_by = $sort_th." ".$sort_type;
            $user_list = $this->user_model->getUserList($where,$length,$start,$order_by);
           
            $this->load->model('user_level_model');
            $user_tmp=array();
            $authority = $this->authority;
            foreach($user_list as $key => $value) {
                $where1='FIND_IN_SET (\''.$value['id'].'\',parent_path) and id !='.$value['id'];
                $data['start_time']?$where1.=' and s_time >='.strtotime($data['start_time']):'';
                $data['end_time']?$where1.=' and user.s_time <='.strtotime($data['end_time']):'';
                $user_tmp=$this->user_model->getFeildList($where1,'count(user.id) as total_next_count ,sum(user.price) as total_price');
                if(isset($user_tmp[0]['total_next_count'])){
                     $user_list[$key]['next_count']=$user_tmp[0]['total_next_count']?$user_tmp[0]['total_next_count']:0;
                     $user_list[$key]['performance']=$user_tmp[0]['total_price']?$user_tmp[0]['total_price']:0;
                }
                if(!$value['color']){
                    $value['color']='green';
                }
                $user_list[$key]['user_level_name']='<b style="color:'.$value['color'].';">'.$value['user_level_name'].'</b>';
                $user_list[$key]['create_time']=date('Y-m-d',$value['create_time']);
                $user_list[$key]['s_time']=date('Y-m-d',$value['s_time']);
                $port = unserialize($value['areas']);
                $user_list[$key]['areas'] = $port['detail'].$value['address'];
                $user_list[$key]['avatar'] = '<img src = "'.$value['avatar'].'" style="height:30px;"/>';
                $edit_url='';
                $view_url = '<a href="/admin/statistic/user_list/?id='.$value['id'].'&start_time='.$data['start_time'].'&end_time='.$data['end_time'].'"><span class="btn btn-xs btn-success">查看</span></a>';
                
                $user_list[$key]['operate'] = $view_url;
            }
            $aaData = $user_list;
    
        }
        $output['aaData'] = $aaData;
        $output['sEcho'] = $_GET['sEcho'];
        $output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
        $output['iTotalRecords'] = $this->data['count']; //总共有几条数据
        echo json_encode($output); //最后把数据以json格式返回
    }
}